//
//  WeightPickerView.m
//  project
//
//  Created by 圆圆陈 on 2018/1/20.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "FinishDayPickerView.h"

@interface FinishDayPickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) NSString        *returnStr;
@property (nonatomic, strong) NSString        *timeStr;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) NSMutableArray *lbarray;
@property (nonatomic, assign) NSInteger min;
@property (nonatomic, assign) NSInteger max;

@end

@implementation FinishDayPickerView {
    UIView       *backView;
    UIPickerView *weightPicker;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _returnStr               = [NSString string];
        _array                   = [NSMutableArray array];
        _lbarray                 = [NSMutableArray array];
        backView                 = [[UIView alloc]initWithFrame:CGRectMake(0, HHHHEIGHT*2/3, HHHWIDTH, HHHHEIGHT/3)];
        backView.backgroundColor = [UIColor whiteColor];
        [self addSubview:backView];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(FitW(10), FitH(10), FitW(80), FitH(30));
        [cancelBtn setTitleNormal:HHH_Text(@"退出")];
        [cancelBtn setTitleColorNormal:SNCOLORBLACK];
        [cancelBtn addTouchUpTarget:self action:@selector(cancelBtnClick)];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        CGSize titleSize = [cancelBtn.titleLabel.text sizeWithFont:cancelBtn.titleLabel.font maxSize:CGSizeMake(MAXFLOAT, FitH(30))];
        cancelBtn.frame = CGRectMake(FitW(10), FitH(10), titleSize.width, FitH(30));
        [backView addSubview:cancelBtn];
        
        UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sureBtn.frame = CGRectMake(HHHWIDTH-FitW(70), FitH(10), FitW(50), FitH(30));
        [sureBtn setTitleNormal:HHH_Text(@"确定")];
        sureBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
        [sureBtn setTitleColorNormal:SNCOLORBLACK];
        [sureBtn addTouchUpTarget:self action:@selector(sureBtnClick)];
        sureBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(17)];
        [backView addSubview:sureBtn];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, FitH(50), HHHWIDTH, 1)];
        line.backgroundColor = SNCOLORGRAYLINE;
        [backView addSubview:line];
        
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(FitW(10), line.bottom+5, HHHWIDTH-FitW(20), FitH(40))];
        label2.font          = [UIFont systemFontOfSize:FitFont(12)];
        label2.numberOfLines = 3;
        label2.lineBreakMode = NSLineBreakByWordWrapping;
        label2.textAlignment = NSTextAlignmentLeft;
        [backView addSubview:label2];
        
        
        weightPicker            = [[UIPickerView alloc] initWithFrame:CGRectMake(0, line.bottom+FitH(20), HHHWIDTH, backView.height-FitH(90))];
        weightPicker.delegate   = self;
        weightPicker.dataSource = self;
        [backView addSubview:weightPicker];
        
        NSInteger bmr = 0;
        NSInteger weight = SN_PublicClass.weightSet;
        if (SN_USER.gender == 1) {
            bmr = 10*weight +6.25*SN_USER.height - 5*SN_USER.age +5;
        }
        else
        {
            bmr = 10*weight +6.25*SN_USER.height - 5*SN_USER.age -161;
        }
        
        _max = bmr *1.5;
        _min = bmr *0.8;
        
        NSInteger min = [KK_ShareData getNumberWith:_min]/100;
        NSInteger max = [KK_ShareData getNumberWith:_max]/100;
        
        for (int i = min; i <= max; i++) {
            [_array addObject:[NSString stringWithFormat:@"%d", i*100]];
        }
        
        NSString  *calorString = [NSString stringWithFormat:@" %d",[KK_ShareData getNumberWith:bmr]];
        NSString  *infoString  = [NSString stringWithFormat:@"%@ %@", SN_Text(@"根据您的健康情况和体重目标,建议日均摄入卡路里约为"), calorString];
        label2.text = infoString;
        [label2 setAttributedText:[SN_PublicClass labelSelfText:label2.text withChangeText:calorString withFontsize:14.0 withChangeColor:[UIColor blackColor]]];
        
    }
    return self;
}

- (void)setWeight:(NSInteger)weight {
    NSInteger row = (weight - _min)/100;
    if (row <0) {
        row = 0;
    }
    [weightPicker selectRow:row inComponent:0 animated:NO];
    _returnStr = [_array objectAtIndex:row];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return _array.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 50;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *pickerLabel = (UILabel *)view;
    
    if (!pickerLabel) {
        pickerLabel               = [[UILabel alloc] init];
        pickerLabel.font          = SNFONT(FitSimpleSize(20, 20, 30, 30, 30));
        pickerLabel.textColor     = SNCOLORBLACK;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    
    //在该代理方法里添加以下两行代码删掉上下的黑线
    [[pickerView.subviews objectAtIndex:1] setHidden:YES];
    [[pickerView.subviews objectAtIndex:2] setHidden:YES];
    return pickerLabel;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [_array objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _returnStr = [_array objectAtIndex:row];
}

- (void)cancelBtnClick {
    [self animationedDown];
}

- (void)sureBtnClick {
    _timeStr = [[NSDate date] dateToDayString];
    if (self.sureButtonBlock) {
        self.sureButtonBlock(_returnStr, _timeStr);
    }
    [self animationedDown];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:backView]) {
        return NO;
    }
    return YES;
}

@end
